<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:include page="../Common/header.jsp"></jsp:include>

<!-- 确认删除模态框 -->


<div id="delDialog" class="modal">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">×</span>
				</button>
				<h4 class="modal-title">提示</h4>
			</div>
			<div class="modal-body">
				<p>是否确认删除</p>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default pull-left"
					data-dismiss="modal">取消</button>
				<button onclick="del()" type="button" class="btn btn-primary">确认</button>
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>

<!-- 确认删除模态框 -->

<!-- 新增模态框 -->


<div id="addDialog" class="modal">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">×</span>
				</button>
				<h4 class="modal-title">添加菜单</h4>
			</div>
			<div class="modal-body">
			<form  id="addMenuform" action="<c:url value="/Menu/add" />"
							method="post" class="well form-horizontal clearfix">
						
							<div id="errorMsgDiv" class="form-group col-xs-12">
								<div class="col-sm-8 col-sm-offset-4" style="font-weight: bold;color: red;" ></div>
							</div>
						
						<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="name">名称*：</label>
								<div class="col-sm-8">
									<input class="form-control" type="text" id="name" name="name" />
								</div>
							</div>
							
							<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="pPmenu">父节点*：</label>
								<div class="col-sm-8">
									<input class="form-control" type="text" disabled="disabled" id="pMenuName" />
								</div>
							</div>
							
						
							<input type="hidden"  name="pid" value="0" id="pid" />
							
							<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="url">地址：</label>
								<div class="col-sm-8">
									<input class="form-control" type="text" id="url" name="url" />
								</div>
							</div>
							
							<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="icon">图标：</label>
								<div class="col-sm-8">
									<input class="form-control" type="text" id="icon" name="icon" />
								</div>
							</div>
							
							<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="sequence">排序：</label>
								<div class="col-sm-8">
									<input class="form-control" type="text" id="sequence" name="sequence" />
								</div>
							</div>
						

							<div class="form-group col-xs-12" id="role_detail">
								<label class="col-sm-4 control-label" for="is_del">状态：</label>
								<div class="col-sm-8">
									<select class="form-control" name="status" id="status">
										<option value="true">启用</option>
										<option value="false">禁用</option>
									</select>
								</div>
							</div>
							
							<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="remark">备注：</label>
								<div class="col-sm-8">
									<textarea class="form-control" id="remark" name="remark" 
										rows="5"></textarea>
								</div>
							</div>
						</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default pull-left"
					data-dismiss="modal">取消</button>
				<button type="button" onclick="add()" class="btn btn-primary">确认</button>
			</div>
		</div>
		<!-- /.modal-content -->
	</div>
	<!-- /.modal-dialog -->
</div>

<!-- 新增模态框 -->

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">	
		<section class="content-header">
		<h1>菜单管理</h1>
		<ol class="breadcrumb">
			<li><a href="<c:url value="/Admin/index" />"><i
					class="fa fa-dashboard"></i> Home</a></li>

			<c:forEach items="${routers}" var="router">
				<li><i class="${router.icon}"></i><a href="javascript:void(0)">&nbsp&nbsp${router.name
						}</a></li>
			</c:forEach>

		</ol>
	</section>

	<section class="content">
		<div class="page-header"></div>
		<div class="operate panel panel-default">
			<div class="panel-body">
				<div class="pull-right">
					<a class="btn btn-sm btn-primary" data-toggle="modal" data-target="#addDialog" >新增</a> <a
						onclick="save()" class="btn btn-sm btn-primary">保存</a> | <a
						data-toggle="modal" data-target="#delDialog"
						class="btn btn-sm btn-danger">删除</a>
				</div>
			</div>
		</div>
		<!-- 功能操作区域结束 -->
		<!-- 列表显示区域  -->
		<div class="box box-danger">
			<div class="box-header with-border">
				<h3 class="box-title"></h3>

				<div class="box-tools pull-right">
					<button type="button" class="btn btn-box-tool"
						data-widget="collapse">
						<i class="fa fa-minus"></i>
					</button>
					<button type="button" class="btn btn-box-tool" data-widget="remove">
						<i class="fa fa-remove"></i>
					</button>
				</div>
			</div>
			<div class="box-body">
				<div class="row">
					<div class="col-sm-4 sub_left_menu ">
					<ul  id="treeDemo" style="overflow: scroll;height: 720px;"
							class="ztree"></ul>

					</div>

					<div class="col-sm-8 last sub_content">
						<form  id="updateMenuform" action="<c:url value="/Menu/update" />"
							method="post" class="well form-horizontal clearfix">
						
							<div id="errorMsgDiv" class="form-group col-xs-12">
								<div class="col-sm-8 col-sm-offset-4" style="font-weight: bold;color: red;" ></div>
							</div>
						
						<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="name">名称*：</label>
								<div class="col-sm-8">
									<input class="form-control" type="text" id="name" name="name" />
								</div>
							</div>
							
							<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="url">地址：</label>
								<div class="col-sm-8">
									<input class="form-control" type="text" id="url" name="url" />
								</div>
							</div>
							
							<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="icon">图标：</label>
								<div class="col-sm-8">
									<input class="form-control" type="text" id="icon" name="icon" />
								</div>
							</div>
							
							<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="sequence">排序：</label>
								<div class="col-sm-8">
									<input class="form-control" type="text" id="sequence" name="sequence" />
								</div>
							</div>
						

							<div class="form-group col-xs-12" id="role_detail">
								<label class="col-sm-4 control-label" for="is_del">状态：</label>
								<div class="col-sm-8">
									<select class="form-control" name="status" id="status">
										<option value="true">启用</option>
										<option value="false">禁用</option>
									</select>
								</div>
							</div>
							
							<div class="form-group col-xs-12">
								<label class="col-sm-4 control-label" for="remark">备注：</label>
								<div class="col-sm-8">
									<textarea class="form-control" id="remark" name="remark" 
										rows="5"></textarea>
								</div>
							</div>
							<input type="hidden" id="id" name="id" value="">
							<input type="hidden" id="level" name="level" value="">
						</form>
					</div>

				</div>
			</div>
		</div>

	</section>

</div>
<!-- /.content-wrapper -->
<jsp:include page="../Common/footer.jsp"></jsp:include>
<script type="text/javascript">
	

	function del() {
		if ($("#updateMenuform #id").val() == "") {
			alert("请先选择要删除的菜单");
			return;
		}
		window.location.href = "<c:url value="/Menu/delete" />" + "?id="
				+ $("#updateMenuform #id").val();
	}
	
	
	function save() {
		
		var errorMsg=$("#updateMenuform #errorMsgDiv div");
		
		if($("#updateMenuform #id").val()==null||$("#updateMenuform #id").val()=="")
		 {
			errorMsg.html("请先选择需要修改的菜单!");
			return false;
		 }
		if(isNaN($("#updateMenuform #sequence").val()))
			{
			   errorMsg.html("排序只能填写数字!");
			   return false;
			}
		
		errorMsg.html("");
		$("#updateMenuform").submit();
	} 
	
	var setting = {
			callback : {
				onClick : zTreeOnClick
			},
			data : {
				simpleData : {
					enable : true                                                                                                                                                                                                                                                                                                                                                            
				}
			},
			
		};

		var zNodes = ${menus};
		var code;

		$.fn.zTree.init($("#treeDemo"), setting, zNodes);
		
		 $.ajaxSetup({   
	           contentType:"application/x-www-form-urlencoded;charset=utf-8",   
	           complete:function(XMLHttpRequest,textStatus){ 
	             var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); //通过XMLHttpRequest取得响应头，sessionstatus，  
	                 if(sessionstatus=="timeout"){ 
	                     alert("登录超时,请重新登录！");
	                 //如果超时就处理 ，指定要跳转的页面  
	                   window.location.reload();   
	                 }   
	              }   
	         });  

		function zTreeOnClick(event, treeId, treeNode) {
			$("#rolesTable tbody tr").attr("class", "");
			$(this).attr("class", "active");
			
			$.post("<c:url value='/Menu/getMenu' />", {
				id :treeNode.id,
			}, function(data, status) {
				
				
				
				if (data == "" || data == null)
					{return;}
				data = eval(data);
				
				$("#addMenuform #pid").val(data.id);
				$("#addMenuform #pMenuName").val(data.name);
				
				
				$("#updateMenuform #name").val(data.name);
				$("#updateMenuform #url").val(data.url);
				$("#updateMenuform #icon").val(data.icon);
				$("#updateMenuform #sequence").val(data.sequence);
				$("#updateMenuform #status").val(data.status==1?"true":"false");
				$("#updateMenuform #remark").val(data.remark);
				$("#updateMenuform #id").val(data.id);
				$("#updateMenuform #level").val(data.level);
			});

		};
		
		function add(){
			
			var errorMsg=$("#addMenuform #errorMsgDiv div");
			
			if($("#addMenuform #name").val()=="")
				{
				errorMsg.html("请先填写菜单的名称");
				return false;
				}
			
			if($("#updateMenuform #level").val()==3)
			{
			errorMsg.html("菜单不能超过三级");
			return false;
			}
			
			if($("#addMenuform #sequence").val()=="")
			{
				$("#addMenuform #sequence").val(0);
			}
			
			$("#addMenuform").submit();
		}
	
</script>
